<template>
  <div class="student-container">
    <!-- 查询表单 -->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="姓名">
        <el-input v-model="form.name" placeholder="请输入姓名" clearable></el-input>
      </el-form-item>
      <el-form-item label="学号">
        <el-input v-model="form.number" placeholder="请输入学号" clearable></el-input>
      </el-form-item>
      <el-form-item label="班级">
        <el-input v-model="form.className" placeholder="请输入班级" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getStudentList">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" icon="el-icon-plus" @click="dialogVisible = true">新增</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table
      :data="tableData"
      border
      stripe
      style="width: 100%; margin-top: 20px;"
      header-cell-class-name="table-header"
    >
      <el-table-column prop="studentId" label="编号" width="180" align="center"></el-table-column>
      <el-table-column prop="name" label="姓名" width="180" align="center"></el-table-column>
      <el-table-column prop="number" label="学号" align="center"></el-table-column>
      <el-table-column prop="className" label="班级" align="center">
        <template slot-scope="scope">
        <span style="margin-left: 10px">{{ scope.row.grade + scope.row.className }}</span>
      </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="getStudent(scope.row)"
          >编辑</el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="delStudent(scope.row.studentId)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      class="pagination"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>

    <!-- 新增学生对话框 -->
    <el-dialog title="添加学生" :visible.sync="dialogVisible" width="500px" center>
      <el-form ref="form" :model="addform" label-width="80px">
        <el-form-item label="学生姓名" prop="name">
          <el-input v-model="addform.name" placeholder="请输入学生姓名"></el-input>
        </el-form-item>
        <el-form-item label="学生学号" prop="number">
          <el-input v-model="addform.number" placeholder="请输入学生学号"></el-input>
        </el-form-item>
        <el-form-item label="学生班级" prop="number">
          <el-select v-model="addform.classId" placeholder="请选择班级">
            <el-option
              v-for="item in Classlist"
              :key="item.id"
              :label="item.className"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addStudent">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 修改学生对话框 -->
    <el-dialog title="修改学生" :visible.sync="editstu" width="500px" center>
      <el-form ref="form" :model="editform" label-width="80px">
        <el-form-item label="学生ID" prop="studentId">
          <el-input v-model="editform.studentId" placeholder="请输入学生ID" disabled></el-input>
        </el-form-item>
        <el-form-item label="学生姓名" prop="name">
          <el-input v-model="editform.name" placeholder="请输入学生姓名"></el-input>
        </el-form-item>
        <el-form-item label="学生学号" prop="number">
          <el-input v-model="editform.number" placeholder="请输入学生学号"></el-input>
        </el-form-item>
        <el-form-item label="学生班级" prop="className">
          <el-input v-model="editform.className" placeholder="请输入学生班级"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="editStudent">确 定</el-button>
        <el-button @click="editstu = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getStudentList, addStudent, editStudent, delStudent, getClass } from "@/api/student/student.js";

export default {
  data() {
    return {
      Classlist: [],
      tableData: [],
      pageNum: 1,
      pageSize: 10,
      total: 0,
      form: {
        name: "",
        number: "",
        className: "",
      },
      addform: {
        name: "",
        number: "",
        classId:"",
      },
      editform: {
        studentId: "",
        name: "",
        number: "",
        className: "",
      },
      dialogVisible: false,
      editstu: false,
    };
  },
  mounted() {
    this.getStudentList();
    this.getClass();
  },
  methods: {
    getStudent(row) {
      this.editstu = true;
      this.editform = {
        ...row
      }
    },
    // 获取班级
    getClass() {
      getClass().then((res) => {
        this.Classlist = res.rows;
        // console.log(res);
      });
    },
    // 获取学生列表
    getStudentList() {
      getStudentList({
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        ...this.form,
      }).then((res) => {
        console.log(res);
        this.tableData = res.rows;
        this.total = res.total;
      });
    },

    // 添加学生
    addStudent() {
      addStudent({
        ...this.addform,
      }).then((res) => {
        this.$message.success("新增成功");
        this.dialogVisible = false;
        this.getStudentList(); // 刷新列表
      });
    },

    // 修改学生
    editStudent() {
      editStudent({
        ...this.editform,
      }).then((res) => {
        this.$message.success("修改成功");
        this.editstu = false;
        this.getStudentList(); // 刷新列表
      });
    },
    // 删除学生
    delStudent(id) {
      delStudent({
        id,
      }).then((res) => {
        this.$message.success("删除成功");
        this.getStudentList(); // 刷新列表
      });
    },

    // 分页大小改变
    handleSizeChange(val) {
      this.pageSize = val;
      this.getStudentList();
    },

    // 当前页改变
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getStudentList();
    },
  },
};
</script>

<style scoped>
.student-container {
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.demo-form-inline {
  margin-bottom: 20px;
}

.demo-form-inline .el-form-item {
  margin-right: 20px;
}

.table-header {
  background-color: #f5f7fa;
  color: #333;
  font-weight: bold;
}

.el-table {
  border-radius: 8px;
  overflow: hidden;
}

.pagination {
  margin-top: 20px;
  text-align: right;
}

.el-dialog__header {
  background-color: #f5f7fa;
  padding: 15px;
  border-bottom: 1px solid #ebeef5;
}

.el-dialog__title {
  font-size: 16px;
  color: #333;
}

.dialog-footer {
  text-align: right;
}
</style>